<script lang="ts" setup>
import {computed, onMounted, reactive, ref} from 'vue'
import {message} from 'ant-design-vue'
import dayjs from 'dayjs'
import {deleteUser, listUserVoByPage} from "@/api/userController.ts";
import {listBlogVoByPage} from "@/api/blogController.ts";

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 60,
    // fixed: 'left'
  },
  {
    title: '标题',
    dataIndex: 'title',
    width: 100,
  },
  {
    title: '发布用户',
    dataIndex: 'username',
    width: 100,
  },
  {
    title: '用户id',
    dataIndex: 'userId',
    width: 100,
  },
  {
    title: '标签',
    dataIndex: 'tags',
    width: 100
  },
  {
    title: '点赞数',
    dataIndex: 'likeNum',
    width: 100
  },
  {
    title: '收藏数',
    dataIndex: 'collectNum',
    width: 100
  },
  {
    title: '评论数',
    dataIndex: 'commentCount',
    width: 100
  },
  {
    title: '浏览量',
    dataIndex: 'viewCount',
    width: 100
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 120
  },
  {
    title: '操作',
    key: 'action',
    width: 100,
    // fixed: 'right'
  },
]

// 定义数据
const dataList = ref<API.UserVO[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.BlogQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'create_time',
  sortOrder: 'ascend',
})

// 获取数据
const fetchData = async () => {
  const res = await listBlogVoByPage({
    ...searchParams,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 表格变化之后，重新获取数据
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUser({id})
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    await fetchData()
  } else {
    message.error('删除失败')
  }
}
</script>
<template>
  <div id="UserManagePage">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" allow-clear/>
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.username" placeholder="输入用户名" allow-clear/>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"/>
    <!-- 表格 -->
    <!--    <a-table :columns="columns" :data-source="dataList" :scroll="{ x: 1500, y: 600 }">-->
    <a-table :columns="columns" :data-source="dataList">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="120"/>
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'teach'|| column.dataIndex === 'learn'">
          <a-space :wrap>
            <a-tag v-for="tag in record[column.dataIndex]" :key="tag">
              {{ tag }}
            </a-tag>
          </a-space>
        </template>

        <template v-else-if="column.dataIndex === 'tags'">
          <a-space :wrap>
            <a-tag v-for="tag in record[column.dataIndex]" :key="tag" color="blue">
              {{ tag }}
            </a-tag>
          </a-space>
        </template>
        <template v-else-if="column.dataIndex === 'username'">
          {{ record.userVO.username }}
        </template>
        <template v-else-if="column.dataIndex === 'userId'">
          {{ record.userVO.id }}
        </template>

        <template v-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button danger @click="doDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<style scoped>
#UserManagePage {
  :deep(.ant-table-cell) {
    text-align: center
  }
}
</style>
